//
//  MyVC.m
//  Project
//
//  Created by 冯东辉 on 2018/9/17.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "MyVC.h"
#import "MyCell.h"
#import "AboutVC.h"
#import "GuestureVC.h"
#import "CodeVC.h"
#import "HelpVC.h"

@interface MyVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIImageView *headBaseV;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *data;

@property (nonatomic, strong) NSArray *imageData;

@property (nonatomic, strong) UIView *logoutView;

@end

@implementation MyVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navTitleL.text = @"我";
    [self createBlueNavigationBar:NO];
    
    _data = @[@"退出登录", @"手势密码", @"检测更新", @"应用分享", @"帮助", @"关于"];
    _imageData = @[@"my_logout", @"my_gesture", @"my_checkout", @"my_share", @"my_user_help", @"my_about"];
    
    [self setHeadView];
    
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

#pragma mark - 头部视图
- (void)setHeadView {
    
    _headBaseV = [[UIImageView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, GET_SCREEN_WIDTH(), GET_SCREEN_WIDTH()*(207-64)/375.0f)];
    _headBaseV.image = [UIImage imageNamed:@"my_head_base"];
    [self.view addSubview:_headBaseV];
    
    UIImageView *headV = [[UIImageView alloc] initWithFrame:CGRectMake((GET_SCREEN_WIDTH()-92)/2.0f, 0, 92, 92)];
    headV.image = [UIImage imageNamed:@"my_head_image"];
    [_headBaseV addSubview:headV];
    
    UIImageView *vipImageV = [[UIImageView alloc] initWithFrame:CGRectMake(headV.right-11-6-2, headV.top+10+40, 12, 12)];
    vipImageV.image = [UIImage imageNamed:@"my_vip"];
    [_headBaseV addSubview:vipImageV];
    
    NSString *userPhoneNum = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
    
    UILabel *userPhoneNumL = [[UILabel alloc] initWithFrame:CGRectMake(0, headV.bottom+(_headBaseV.height-headV.bottom-20)/2.0f-12, GET_SCREEN_WIDTH(), 20)];
    userPhoneNumL.text = userPhoneNum;
    userPhoneNumL.textColor = UICOLOR_HEX(0xffffffff);
    userPhoneNumL.textAlignment = NSTextAlignmentCenter;
    userPhoneNumL.font = [UIFont systemFontOfSize:15];
    [_headBaseV addSubview:userPhoneNumL];
}

/**
 * 初始化视图
 */
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _headBaseV.bottom, GET_SCREEN_WIDTH(), GET_SCREEN_HEIGHT()-_headBaseV.bottom-SafeAreaBottomHeight)];
        _tableView.backgroundColor = UICOLOR_HEX(0xffEDEEEF);
        _tableView.bounces = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

#pragma mark - 展示选择框
- (void)showLogoutView {
    
    if (_logoutView) {
        
        _logoutView.hidden = NO;
        return;
    }
    
    _logoutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GET_SCREEN_WIDTH(), GET_SCREEN_HEIGHT())];
    _logoutView.backgroundColor = [UICOLOR_HEX(0xff000000) colorWithAlphaComponent:0.5f];
    [self.view.window addSubview:_logoutView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenLogoutView)];
    [_logoutView addGestureRecognizer:tap];
    
    UIView *middleV = [[UIView alloc] initWithFrame:CGRectMake(GET_SCREEN_WIDTH()/4.0f, (GET_SCREEN_HEIGHT()-150)/2.0f, GET_SCREEN_WIDTH()/2.0f, 150)];
    middleV.backgroundColor = UICOLOR_HEX(0xffffffff);
    middleV.layer.cornerRadius = 10;
    middleV.layer.masksToBounds = YES;
    [_logoutView addSubview:middleV];
    
    NSArray *btn_title = @[@"切换账号", @"退出应用", @"取 消"];
    
    for (int i = 0; i < 3; i++) {
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 50*i, middleV.width, 50)];
        [btn setTitle:btn_title[i] forState:UIControlStateNormal];
        [btn setTitleColor:UICOLOR_HEX(0xff000000) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        btn.tag = 1000+i;
        [btn addTarget:self action:@selector(logOutOperate:) forControlEvents:UIControlEventTouchUpInside];
        [middleV addSubview:btn];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 49, btn.width, 1)];
        line.backgroundColor = UICOLOR_HEX(0xffEDEEEF);
        [btn addSubview:line];
        
        if (i == 2) {
            line.hidden = YES;
            [btn setTitleColor:Base_color forState:UIControlStateNormal];
        }
    }
}

#pragma mark - 隐藏退出视图
- (void)hiddenLogoutView {
    
    _logoutView.hidden = YES;
}

#pragma mark - 退出操作
- (void)logOutOperate:(UIButton *)btn {
    
    if (btn.tag == 1000+2) {
        // 取消操作
        [self hiddenLogoutView];
    }else if (btn.tag == 1000 + 1) {
        // 退出应用
        exit(0);
    }else {
        // 切换账号
        [[NSNotificationCenter defaultCenter] postNotificationName:@"QIEHUANZHANGHAO" object:nil];
    }
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height = _tableView.height/6.0f;
    
    if (height < 60) {
        
        return height;
    }
    
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 5) {
        
        // 关于
        AboutVC *vc = [[AboutVC alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == 0) {
        
        // 退出登录
        [self showLogoutView];
        
    }else if (indexPath.row == 1) {
        
        // 手势密码
        GuestureVC *vc = [[GuestureVC alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == 3) {
        
        // 应用分享
        CodeVC *codevc = [[CodeVC alloc] init];
        codevc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:codevc animated:YES];
        
    }else if (indexPath.row == 2) {
        
        // 版本检测
        [self checkNewVersion];
        
    }else {
        
        // 帮助
        HelpVC *vc = [[HelpVC alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier_my = @"my_cell";
    
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier_my];
    
    if (cell == nil) {
        
        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier_my];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = UICOLOR_HEX(0xffEDEEEF);
        cell.contentView.backgroundColor = UICOLOR_HEX(0xffEDEEEF);
    }
    
    [cell reloadDataWith:_data imageData:_imageData indexPath:indexPath];
    
    return cell;
}

#pragma mark - 版本检测
- (void)checkNewVersion {
    
    NSString *app_Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    [[ProjectApi instance] appUpdateIpaCallBack:^(NetModel *object) {
        
        if (object.isSuccessed) {
            
            NSDictionary *dic = [[object.service firstObject] firstObject];
            
            NSString *v_version = [NSString stringWithFormat:@"%@", dic[@"v_version"]];
            
            if (![v_version isEqualToString:app_Version]) {
                
                NSString *v_remark = [NSString stringWithFormat:@"%@", dic[@"v_remak"]];
                NSString *v_path = [NSString stringWithFormat:@"%@", dic[@"v_path"]];
                
                [self showNoteAlertView:v_path content:v_remark];
                
            }else {
                
                [SVProgressHUD showInfoWithStatus:@"当前已是最新版本"];
                [SVProgressHUD dismissWithDelay:1.0f];
            }
        }
    }];
}

- (void)showNoteAlertView:(NSString *)url content:(NSString *)content {
    
    // 初始化
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"检测到有新版本，是否更新？" message:content preferredStyle:UIAlertControllerStyleAlert];
    
    // 取消
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alertController addAction:cancelAction];
    
    // 确定
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        
        [self exit];
    }];
    
    [alertController addAction:confirmAction];
    
    // 弹出
    [self presentViewController:alertController animated:YES completion:^{
    }];
}

- (void)exit {
    
    exit(0);
}

@end
